package org.example;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.params.SetParams;

import java.util.Set;

public class RedisDemoGeneric {
    public static void test1(Jedis jedis){
        System.out.println("Redis的get和set的使用");
        jedis.flushAll() ;
        jedis.set("key","1111");
        jedis.set("key1","2222");
        SetParams params = new SetParams();
        params.ex(10);
        params.xx();
        jedis.set("key", "333", params);
        String key = jedis.get("key");
        System.out.println( key);
    }
    public static void test2(Jedis jedis){
        System.out.println("exists和del");
        jedis.flushAll() ;
        jedis.set("key","1111");
        jedis.set("key1","2222");
        jedis.set("key2","3333");
        boolean result=jedis.exists("key");
        System.out.println(result);
        long result1=jedis.del("key");
        System.out.println(result1);
        result=jedis.exists("key");
        System.out.println(result);
        result1=jedis.del("key1","key2","key");
        System.out.println(result1);
    }
    public static void test4(Jedis jedis){
        System.out.println("expire和ttl");
        jedis.flushAll();
        jedis.set("key","1111");
        jedis.expire("key",10);
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        long time = jedis.ttl("key");
        System.out.println( time);
    }
    public static void test5(Jedis jedis){
        System.out.println("type");
        jedis.flushAll();
        jedis.set("key","1111");
        String type=jedis.type("key");
        System.out.println(type);
        jedis.lpush("key1","1111","2222","3333");
        type=jedis.type("key1");
        System.out.println(type);
        jedis.hset("key3","f1","111");
        type = jedis.type("key3");
        System.out.println(type);
        jedis.sadd("key4","1111","2222","3333");
        type = jedis.type("key4");
        System.out.println(type);
        jedis.zadd("key5",1,"zhangsan");
        type = jedis.type("key5");
        System.out.println(type);
    }
    public static void test3(Jedis jedis){
        jedis.flushAll();
        jedis.set("key", "111");
        jedis.set("key2", "111");
        jedis.set("key3", "111");
        jedis.set("key4", "111");
        Set<String> keys = jedis.keys("*");
        System.out.println( keys);
    }
    public static void main(String[] args) {
        //连接到Redis服务器上
        JedisPool jedisPool=new JedisPool("tcp://127.0.0.1:8888");
        //从redis中获取数据
        try(Jedis jedis=jedisPool.getResource()){
            //获取数据
            String pong= jedis.ping();
            System.out.println(pong);
//            test1(jedis);
//            test2(jedis);
            test3(jedis);
//            test4(jedis);
//            test5(jedis);

        }
    }

}
